
#convert favor variable to 1s and 0s

mturk$favor2 <- mturk$favor
mturk$favor2[mturk$favor == -1] <- 0 


########################################################################################
# Test whether intensity levels differ between treatment and control groups on average
########################################################################################

t.test(abs(subset(mturk, issue=="FOR" & group!="treatment1" & group!="treatment2")$composite), 
		abs(subset(mturk, issue=="FOR" & group=="treatment2")$composite))

t.test(abs(subset(mturk, issue=="FOR" & group!="treatment1" & group!="treatment2")$composite), 
		abs(subset(mturk, issue=="FOR" & group=="treatment1")$composite))

t.test(abs(subset(mturk, issue=="JNK" & group!="treatment1" & group!="treatment2")$composite), 
		abs(subset(mturk, issue=="JNK" & group=="treatment2")$composite))

t.test(abs(subset(mturk, issue=="JNK" & group!="treatment1" & group!="treatment2")$composite), 
		abs(subset(mturk, issue=="JNK" & group=="treatment1")$composite))

t.test(abs(subset(mturk, issue=="NET" & group!="treatment1" & group!="treatment2")$composite), 
		abs(subset(mturk, issue=="NET" & group=="treatment2")$composite))

t.test(abs(subset(mturk, issue=="NET" & group!="treatment1" & group!="treatment2")$composite), 
		abs(subset(mturk, issue=="NET" & group=="treatment1")$composite))

t.test(abs(subset(mturk, issue=="IMM" & group=="control")$composite), 
		abs(subset(mturk, issue=="IMM" & group=="treatment")$composite))

t.test(abs(subset(mturk, issue=="SOC" & group=="control")$composite), 
		abs(subset(mturk, issue=="SOC" & group=="treatment")$composite))
		
t.test(abs(subset(mturk, issue=="VAC" & group=="control")$composite), 
		abs(subset(mturk, issue=="VAC" & group=="treatment")$composite))


AVGintensity <- c(mean(abs(subset(mturk, issue=="FOR" & (group=="control1" | group=="control2"))$composite), na.rm=TRUE),
				mean(abs(subset(mturk, issue=="JNK" & (group=="control1" | group=="control2"))$composite), na.rm=TRUE),
				mean(abs(subset(mturk, issue=="NET" & (group=="control1" | group=="control2"))$composite), na.rm=TRUE),
				mean(abs(subset(mturk, issue=="IMM" & group=="control")$composite), na.rm=TRUE),
				mean(abs(subset(mturk, issue=="SOC" & group=="control")$composite), na.rm=TRUE),
				mean(abs(subset(mturk, issue=="VAC" & group=="control")$composite), na.rm=TRUE))



#############################
# Calculate ATEs and CIs
#############################

ATEs <- c(mean(subset(mturk, issue=="FOR" & group=="treatment2")$favor2, na.rm=TRUE) - 
			mean(subset(mturk, issue=="FOR" & group!="treatment1" & group!="treatment2")$favor2, na.rm=TRUE),
		mean(subset(mturk, issue=="JNK" & group=="treatment2")$favor2, na.rm=TRUE) - 
			mean(subset(mturk, issue=="JNK" & group!="treatment1" & group!="treatment2")$favor2, na.rm=TRUE),
		mean(subset(mturk, issue=="NET" & group=="treatment2")$favor2, na.rm=TRUE) - 
			mean(subset(mturk, issue=="NET" & group!="treatment1" & group!="treatment2")$favor2, na.rm=TRUE),
		-mean(subset(mturk, issue=="IMM" & group=="treatment")$favor2, na.rm=TRUE) + 
			mean(subset(mturk, issue=="IMM" & group=="control")$favor2, na.rm=TRUE),
		-mean(subset(mturk, issue=="SOC" & group=="treatment")$favor2, na.rm=TRUE) + 
			mean(subset(mturk, issue=="SOC" & group=="control")$favor2, na.rm=TRUE),
		-mean(subset(mturk, issue=="VAC" & group=="treatment")$favor2, na.rm=TRUE) + 
			mean(subset(mturk, issue=="VAC" & group=="control")$favor2, na.rm=TRUE))

CIs <- rbind(as.numeric(t.test(subset(mturk, issue=="FOR" & group=="treatment2")$favor2,
 				subset(mturk, issue=="FOR" & group!="treatment1" & group!="treatment2")$favor2)$conf.int),
 		 as.numeric(t.test(subset(mturk, issue=="JNK" & group=="treatment2")$favor2,
 				subset(mturk, issue=="JNK" & group!="treatment1" & group!="treatment2")$favor2)$conf.int),
 		 as.numeric(t.test(subset(mturk, issue=="NET" & group=="treatment2")$favor2,
 				subset(mturk, issue=="NET" & group!="treatment1" & group!="treatment2")$favor2)$conf.int),
 		 as.numeric(t.test(subset(mturk, issue=="IMM" & group=="control")$favor2,
 				subset(mturk, issue=="IMM" & group=="treatment")$favor2)$conf.int),
		 as.numeric(t.test(subset(mturk, issue=="SOC" & group=="control")$favor2,
 				subset(mturk, issue=="SOC" & group=="treatment")$favor2)$conf.int),
		 as.numeric(t.test(subset(mturk, issue=="VAC" & group=="control")$favor2,
 				subset(mturk, issue=="VAC" & group=="treatment")$favor2)$conf.int))
			
		
		
#############################
# Plot it
#############################


plot(AVGintensity, ATEs, pch=c(16, 16, 16, 15, 15, 15), xlim=c(6,8), ylim=c(-.04,.2), ylab="Average Treatment Effect", xlab="Mean Attitude Intensity", axes=FALSE)
axis(1)
axis(2, at=seq(-0.04, .2, by=.04), labels=c(-4, 0, 4, 8, 12, 16, "20%"),  las=2)

legend("bottomleft", legend=c("Study 1","Study 2"), pch=c(16,15), bty="n", cex=.8)

segments(AVGintensity, CIs[,1], AVGintensity, CIs[,2], lwd=.8)
segments(AVGintensity-.02, CIs[,1], AVGintensity+.02, CIs[,1], lwd=.8)
segments(AVGintensity-.02, CIs[,2], AVGintensity+.02, CIs[,2], lwd=.8)

abline(h=0, lty="dotted")

segments(6, coef(lm(ATEs~AVGintensity))%*%c(1, 6), 8, coef(lm(ATEs~AVGintensity))%*%c(1, 8), lty="dashed", lwd=1)

text(AVGintensity[1], ATEs[1], "Foreign Language Reqs", pos=4, cex=.8)
text(AVGintensity[2], ATEs[2], "Junk Food Taxes", pos=2, cex=.8)
text(AVGintensity[3], ATEs[3], "Net Neutrality", pos=4, cex=.8)
text(AVGintensity[4], ATEs[4], "Guest Worker Prgm", pos=4, cex=.8)
text(AVGintensity[5], ATEs[5], "Social Security Reform", pos=2, cex=.8)
text(AVGintensity[6], ATEs[6], "School Vaccine Reqs", pos=2, cex=.8)

#############################
# Calculate slope of the line
#############################

#  b = -0.05446

summary(lm(ATEs~AVGintensity))		


